﻿/**
 * ...
 * @author Natalia Ukhorskaya
 */

package gui.hug;

import flash.events.Event;
import gui.hug.HUGLabel;
import gui.hug.HUGColors;
import gui.hug.priv.HUGNumericStepperButton;

import flash.events.MouseEvent;
import flash.display.Shape;
import flash.display.Sprite;
import flash.display.Loader;
import flash.net.URLRequest;

class HUGNumericStepper extends HUGSprite
{
	private var minValue : Int;
	private var maxValue : Int;
	private var value : Int;
	
	private var label : HUGLabel;

	public function new(width : Int, minValue : Int, maxValue : Int, currentValue : Int) 
	{
		super();
		this.minValue = minValue;
		this.maxValue = maxValue;
		this.width = width;
		this.value = currentValue;
		
		draw();
		
		refresh();
	}
	
	private function draw() {
		this.graphics.lineStyle(1, HUGColors.VK_BORDER_OUTER);
		this.graphics.beginFill(HUGColors.WHITE);
        this.graphics.drawRect(0, 0, width, 20);
        this.graphics.endFill();
		
		var leftButton : HUGNumericStepperButton = new HUGNumericStepperButton(true);
		leftButton.setLocation(0, 0);
		leftButton.addEventListener(MouseEvent.CLICK, leftClick);
		this.addChild(leftButton);
		
		var rightButton : HUGNumericStepperButton = new HUGNumericStepperButton(true);
		rightButton.setLocation(width - 20, 0);
		leftButton.addEventListener(MouseEvent.CLICK, rightClick);
		this.addChild(rightButton);
	}
	
	
	private function leftClick(e : MouseEvent) {
		if (value > minValue) {
			value--;
			refresh();
			dispatchEvent(new Event(Event.CHANGE));
		}
	}
	
	private function rightClick(e : MouseEvent) {
		if (value < maxValue) {
			value++;
			refresh();
			dispatchEvent(new Event(Event.CHANGE));
		}
	}
	
	private function refresh() {
		if (null != label) {
			removeChild(label);
		}
		label = new HUGLabel();
		var str: String = "<font color='" + HUGColors.VK_BUTTON + "'size='12'>" + value + "</font>";
		label.text = str;
		label.setLocation(25, 0);
		addChild(label);
	}
	
	public function getValue(): Int {
		return value;
	}
	
	public function setValue(value : Int) {
		this.value = value;
		refresh();
	}
}